<script>
import { onBeforeMount, reactive, ref } from "vue";
import { Toast } from "vant";
import { useRouter } from "vue-router";
// 引入接口
import { getBookcabinetJsonData } from "../assets/api/bookcabinet.js";
export default {
  setup() {
    const router = useRouter();
    let show = ref(false);
    let spinner = ref(false);
    let vip = ref(false);

    let userinfo = JSON.parse(window.localStorage.getItem("userinfo"));
    if (userinfo.isVip) {
      vip.value = true;
    } else {
      vip.value = false;
    }
    let commItem = reactive({
      randomItem: [],
      data: [],
    });
    // 数据初始化
    let dateInit = async () => {
      let data = await getBookcabinetJsonData();
      console.log(data);
      commItem.data = data;
      randomNum();
    };

    // 随机数据
    let randomNum = () => {
      let i = parseInt(Math.random() * 50);
      let arr = [];
      commItem.data.allList.forEach((item) => {
        arr.push(item);
      });
      commItem.randomItem = arr.splice(i, 5);
    };
    // 挂载前
    onBeforeMount(() => {
      dateInit();
    });

    // 点击确认开通按钮
    let onconfirm = () => {
      spinner.value = true;

      let userinfo = JSON.parse(window.localStorage.getItem("userinfo"));
      userinfo.isVip = true;
      localStorage.setItem("userinfo", JSON.stringify(userinfo));

      setTimeout(() => {
        spinner.value = false;
        Toast("开通成功");
        vip.value = true;
      }, 3000);
    };
    // 点击立即开通会员
    let like = () => {
      show.value = true;
    };

    // 回到上一页
    let back = () => {
      router.go(-1);
    };

    // 换一换
    let change = () => {
      randomNum();
    };
    return {
      show,
      onconfirm,
      like,
      spinner,
      vip,
      back,
      commItem,
      change,
    };
  },
};
</script>

<template>
  <div class="Member">
    <van-sticky>
      <div class="top">
        <span @click="back" class="iconfont icon-xiangzuo1"></span>
        <p>会员中心</p>
        <span class="iconfont icon-xiangzuo1" style="opacity: 0"></span>
      </div>
    </van-sticky>
    <div class="info">
      <div class="img">
        <img src="../assets/img/palin.jpg" alt="" />
      </div>
      <div class="name">
        <p class="me">palin</p>
        <p class="vip">
          <span class="iconfont icon-zuanshihuiyuan-line"></span
          >开通会员海量小说免费读
        </p>
      </div>
    </div>
    <!-- 立即开通 -->
    <div class="like">
      <div class="text" v-show="!vip">
        <p class="no">尚未开通会员</p>
        <p class="liji" @click="like">
          立即开通 <span class="iconfont icon-xiangyou1"></span>
        </p>
      </div>
      <div class="text vips" v-show="vip">
        <p class="yes">已开通会员</p>
        <p class="liji">
          享受会员权益 <span class="iconfont icon-xiangyou1"></span>
        </p>
      </div>
    </div>
    <!-- 会员权益 -->
    <div class="quan">
      <div class="top">
        <p class="one">会员权益</p>
        <p class="two">全部特权</p>
      </div>
      <ul>
        <li>
          <div class="icon">
            <span class="iconfont icon-ziliaoku"></span>
          </div>
          <p>包月书库</p>
        </li>
        <li>
          <div class="icon">
            <span class="iconfont icon-zuanshi"></span>
          </div>
          <p>尊贵身份</p>
        </li>
        <li>
          <div class="icon">
            <span class="iconfont icon-zhekou1"></span>
          </div>
          <p>购买折扣</p>
        </li>
        <li>
          <div class="icon">
            <span class="iconfont icon-f-vip"></span>
          </div>
          <p>超级特权</p>
        </li>
      </ul>
    </div>
    <!-- 为你推荐 -->
    <div class="commit">
      <div class="top">
        <p class="one">为你推荐</p>
        <p class="two" @click="change">换一换</p>
      </div>
      <div class="commit_box">
        <div
          class="commit_item"
          v-for="item in commItem.randomItem"
          :key="item.BookId"
        >
          <div class="img">
            <img :src="item.BookImage" alt="" />
          </div>
          <div class="text">
            <p class="title">{{ item.BookName }}</p>
            <p class="content">
              {{ item.Description }}
            </p>
            <div class="bottom">
              <p class="name">{{ item.Author }}</p>
              <p class="cateory">{{ item.tagList[item.tagList.length - 1] }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <van-dialog
      v-model:show="show"
      title="是否开通会员"
      @confirm="onconfirm"
      show-cancel-button
    >
    </van-dialog>
    <van-loading type="spinner" v-show="spinner" />
  </div>
</template>

<style lang="scss" scoped>
.Member {
  min-height: 100%;
  min-width: 375px;
  max-width: 750px;
  width: 100%;
  box-sizing: border-box;
  padding-bottom: 65px;
  .top {
    display: flex;
    padding: 0 13px;
    box-sizing: border-box;
    justify-content: space-between;
    height: 50px;
    align-items: center;
    color: #5e5f61;
    background-color: #fff;
    p {
      font-weight: bold;
    }
  }
  .icon-xiangzuo1 {
    font-size: 20px;
  }
  .info {
    display: flex;
    height: 70px;
    padding: 0 13px;
    box-sizing: border-box;
    width: 100%;
    align-items: center;
    .img {
      width: 60px;
      height: 60px;
      border-radius: 50%;
      overflow: hidden;
    }
    .name {
      display: flex;
      flex-direction: column;
      margin-left: 17px;
      height: 100%;
      justify-content: space-evenly;
      .me {
        font-size: 16px;
        height: 28px;
        line-height: 28px;
      }
      .vip {
        display: flex;
        color: #c48a5c;
        align-items: center;
        font-size: 12px;
        span {
          margin-right: 3px;
        }
      }
    }
  }
  .like {
    margin-top: 20px;
    height: 54px;
    width: 100%;
    padding: 0 13px;
    box-sizing: border-box;
    background-image: linear-gradient(to bottom, transparent, #eee);
    .text {
      width: 100%;
      background: url(../assets/img/vip.png) no-repeat;
      height: 100%;
      background-size: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 14px;
      box-sizing: border-box;
      .no {
        color: #fff;
        font-weight: bold;
        font-size: 17px;
      }
      .yes {
        color: #d09c5c;
        font-weight: bold;
        font-size: 17px;
      }
      .liji {
        color: #fff;
        font-size: 14px;
        display: flex;
        align-items: flex-end;
      }
    }
    .vips {
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      background-image: linear-gradient(to right, #ffe4c2, #d09c5c);
    }
  }
  .quan {
    margin-top: 20px;
    padding: 0 13px;
    .top {
      display: flex;
      align-items: center;
      .one {
        font-weight: bold;
        font-size: 18px;
      }
      .two {
        font-size: 15px;
        color: #d09c5c;
        font-weight: normal;
      }
    }
    ul {
      display: flex;
      justify-content: space-around;
      li {
        display: flex;
        flex-direction: column;
        align-items: center;
        .icon {
          width: 48px;
          height: 48px;
          background-color: #f9f8f4;
          border-radius: 50%;
          overflow: hidden;
          display: flex;
          align-items: center;
          justify-content: center;
          span {
            font-size: 20px;
            color: #d4d3cf;
            line-height: 48px;
            text-align: center;
          }
        }
        p {
          font-size: 12px;
          color: #8e8e8e;
          margin-top: 6px;
        }
      }
    }
  }
  .commit {
    margin-top: 20px;
    padding: 0 13px;
    .top {
      display: flex;
      align-items: center;
      .one {
        font-weight: bold;
        font-size: 18px;
      }
      .two {
        font-size: 15px;
        color: #d09c5c;
        font-weight: normal;
      }
    }
    .commit_box {
      margin-top: 10px;
      .commit_item {
        display: flex;
        margin-top: 7px;
        .img {
          width: 110px;
          height: 144px;
          border-radius: 6px;
          overflow: hidden;
          flex-shrink: 0;
        }
        .text {
          display: flex;
          flex-direction: column;
          margin-left: 10px;
          height: 144px;
          justify-content: space-around;
          border-bottom: 1px solid #f7f7f7;
          padding-bottom: 7px;
          .title {
            font-size: 17px;
            font-weight: bold;
          }
          .content {
            color: #a4a8ab;
            font-size: 13px;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .bottom {
            display: flex;
            justify-content: space-between;
            .name {
              font-size: 12px;
              color: #b1b1b1;
            }
            .cateory {
              color: #291939;
              height: 22px;
              line-height: 22px;
              padding: 0 8px;
              text-align: center;
              background-color: #f5f5f5;
              border-radius: 5px;
              font-size: 13px;
            }
          }
        }
      }
    }
  }
  .van-loading {
    color: #d09c5c;
    position: fixed;
    top: 50%;
    left: 46%;
  }
}
</style>